package controller.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.service.InventoryService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import vo.InventoryVO;
import vo.ListVO;

public class InventoryAction extends DispatchAction {

	public ActionForward selectList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		int pageNo=1;
		String pageNoTemp=request.getParameter("pageNo");
		String searchDate=request.getParameter("searchDate");
		String searchProduct=request.getParameter("searchProduct");
				
		try{
			pageNo=Integer.parseInt(pageNoTemp);
		} catch (NumberFormatException ne){}
		
		HttpSession session=request.getSession();
		String id=(String)session.getAttribute("id");
				
		InventoryService service=InventoryService.getInstance();	
		ListVO lvo=service.selectAllInventory(pageNo, id, searchDate, searchProduct);
					 
		request.setAttribute("lvo", lvo);		
		request.setAttribute("searchDate", searchDate);	
		request.setAttribute("searchProduct", searchProduct);	
		return mapping.findForward("inventorylist");
		
	}
	
	public ActionForward selectDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String findId=request.getParameter("id");	
		String stockNo=request.getParameter("stockNo");	
		String pageNo=request.getParameter("pageNo");
		String searchDate=request.getParameter("searchDate");
		String searchProduct=request.getParameter("searchProduct");
		
		InventoryService service=InventoryService.getInstance();			
		InventoryVO ivo=service.selectInventoryDetailOfStock(findId, stockNo);				
		List list=service.selectInventoryDetailOfSale(findId, stockNo);
		
		request.setAttribute("ivo", ivo);
		request.setAttribute("list", list);		
		request.setAttribute("pageNo", pageNo);	
		request.setAttribute("searchDate", searchDate);	
		request.setAttribute("searchProduct", searchProduct);	
					
		return mapping.findForward("inventoryDetail");
	}
	
	public ActionForward selectBestItemList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		HttpSession session=request.getSession();
		String id=(String)session.getAttribute("id");
		
		InventoryService service=InventoryService.getInstance();		
					
		List list=service.selectBestItemList(id);		
		request.setAttribute("list", list);				
					
		return mapping.findForward("bestList");
	}
		
}